package com.yukefms.rule.formula.basis;

import com.yukefms.engine.running.MatchedStatus;
import com.yukefms.engine.running.RunningContext;
import com.yukefms.jointpoint.*;
import com.yukefms.rule.formula.interfaces.AbstractStaticBAFormula;
import com.yukefms.rule.formula.interfaces.DynamicBAFormula;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * @Description the formula is to specify body sitting down action, which is dynamic action and should be meausred by current
 * point cloud and previous one. Currently, the previous one is set by at most the 10th old one, which is called
 * LAST_NUM configured in {@code EngineConfig}.
 * @Author Ping
 * @Date 2021/7/16 0:33
 **/
public class KeepNoMovingDynamicBAFormula extends AbstractStaticBAFormula implements DynamicBAFormula {
    public static Logger logger = LogManager.getLogger(KeepNoMovingDynamicBAFormula.class);

    /**
     * here use spineBaseJointPoint to define sitDownAction.
     */
    private String  jointPoint ; // "0"


    public KeepNoMovingDynamicBAFormula() {
        super(Kinect25JointType.JointType_SpineMid.toString()) ;
        // core body point is the default.
        this.jointPoint = Kinect25JointType.JointType_SpineMid.toString() ;
    }
    public KeepNoMovingDynamicBAFormula(String jointPoint) {
        super(jointPoint) ;
        // core body point is the default.
        this.jointPoint = jointPoint ;
    }

    @Override
    public MatchedStatus matched(BodyPointCloud bodyPointCloud, RunningContext context) {
        /*
        Comparing current pointcloud with LAST ONE on JOINT_TYPE_SpineBase.
        If current one is under below of last one, it was right.
         */

 /*       if (!context.hasCachedLastPosition()) {
            logger.error("NOTE: The cached pointCloud structure is empty now.") ;
        } else */
      if (GeometryAdaptorForPointCalculator.distanceOfTwoPositions(bodyPointCloud.getPosition(jointPoint),
                context.getLastPosition(jointPoint)) < TolerantEpsilon.STATIC_LENGTH_EPSILON) {
            // if this formula has successor , then the matching result is decided by its successor.
            if (this.successor != null) {
                return this.successor.matched(bodyPointCloud, context);
            } else {
                return MatchedStatus.TRUE;
            }
        }
        return MatchedStatus.FALSE;
    }
}
